package com.sxt.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sxt.common.CodeMsg;
import com.sxt.common.Result;
import com.sxt.domain.BusCar;
import com.sxt.mapper.BusCarDao;
import com.sxt.query.BusCarQuery;
import com.sxt.service.BusCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: 车辆业务接口实现
 */
@Service
public class BusCarServiceImpl implements BusCarService {

    @Autowired
    private BusCarDao busCarDao;

    /**
     * 车辆的分页查询
     *
     * @param query
     * @return
     */
    @Override
    public Result queryPage(BusCarQuery query) {
        Page<BusCar> busCarPage = PageHelper.startPage(query.getPage(), query.getLimit());
        busCarDao.selectList(query);
        return new Result(busCarPage.toPageInfo());
    }

    /**
     * 新增车辆
     *
     * @param busCar
     * @return
     */
    @Override
    public Result add(BusCar busCar) {
        // 进行业务数据校验
        // 车牌号不能重复
        BusCar car = busCarDao.selectOneByNum(busCar.getNum());
        if (car != null) {
            return new Result(CodeMsg.CAR_NUM_EXIST_ERROR);
        }
        busCarDao.insert(busCar);
        return new Result();
    }
}
